import React, { Component } from 'react'
import { Card, Button, Checkbox, Form, Input, message } from 'antd'
import logo from '@/assets/logo.png'
import styles from './index.module.scss'
import { login } from '@/api/login'
import { setToken } from '@/utils/storage'
export default class Login extends Component {
  render() {
    console.log(this.props)
    return (
      <div className={styles.root}>
        <Card className="login-container">
          <img className="login-logo" src={logo} alt="null" />
          {/* 登录表单 */}
          <Form
            size="large"
            // 表单的初始值
            initialValues={{
              mobile: '13911111111',
              code: '246810',
              agree: true,
            }}
            autoComplete="off"
            validateTrigger={['onBlur', 'onChange']}
            onFinish={this.onFinish}
          >
            <Form.Item
              name="mobile"
              rules={[
                { required: true, message: '手机号码必填' },
                {
                  pattern: /^1[3-9]\d{9}$/,
                  message: '手机号码格式不对',
                },
              ]}
            >
              <Input
                size="large"
                placeholder="请输入手机号"
                autoComplete="off"
              />
            </Form.Item>

            <Form.Item
              name="code"
              rules={[
                { required: true, message: '请输入验证码' },
                {
                  len: 6,
                  message: '验证码长度必须是6个数字',
                  validateTrigger: 'onBlur',
                },
                /* {
                  validator: (rules, value) =>
                    value.length === 6
                      ? Promise.resolve()
                      : Promise.reject(new Error('请输入6个字符')),
                }, */
              ]}
            >
              <Input
                size="large"
                placeholder="请输入验证码"
                maxLength={6}
                autoComplete="off"
              />
            </Form.Item>

            <Form.Item
              rules={[
                {
                  // validator自定义校验规则：参数1:规则对象;参数2：表单的值
                  validator: (rules, value) => {
                    // 返回值要求是Promise
                    return value
                      ? Promise.resolve()
                      : Promise.reject(new Error('请阅读并同意用户协议'))
                  },
                },
              ]}
              name="agree"
              valuePropName="checked"
            >
              <Checkbox className="login-checkbox-label">
                我已阅读并同意「隐私条款」和「用户协议」
              </Checkbox>
            </Form.Item>

            <Form.Item>
              <Button block type="primary" htmlType="submit">
                登录
              </Button>
            </Form.Item>
          </Form>
        </Card>
      </div>
    )
  }

  // 登录
  onFinish = async (values) => {
    // return
    const { mobile, code } = values
    try {
      const res = await login(mobile, code)
      // 提示信息
      message.success('登录成功', 2, () => {
        // 登录成功，将token存储到本地缓存中
        setToken('Bearer ' + res.data.token)
        // 跳转到指定的页面
        this.props.history.push(this.props.location.state?.from || '/')
      })
    } catch (err) {
      // console.log(err)
      message.error(err.response.data.messag, 3)
    }
  }
}
